package com.qf.ebooks.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ebooks.entity.EbookEntry;
import com.qf.ebooks.mapper.EbookEntryMapper;
import com.qf.ebooks.service.EbookEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author songfei
 * @Date 2021/10/7 13:23
 * @Version 1.0
 */
@Service
public class EbookEntryServiceImpl implements EbookEntryService {
    @Autowired
    private EbookEntryMapper ebookEntryMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void insert(EbookEntry ebookEntry) {
        int i = ebookEntryMapper.insertSelective(ebookEntry);
        if(i!=1){
            System.out.println("添加书信息失败");
            throw new RuntimeException("添加书信息失败");
        }

    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(EbookEntry ebookEntry) {
        int i = ebookEntryMapper.updateByPrimaryKeySelective(ebookEntry);
        if(i!=1){
            System.out.println("修改书信息失败");
            throw new RuntimeException("修改书信息失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Integer id) {
        int i = ebookEntryMapper.deleteByPrimaryKey(id);
        if(i!=1){
            System.out.println("删除书信息失败");
            throw new RuntimeException("删除书信息失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public PageInfo<EbookEntry> findByEbookCategoryIdAndPage(Integer page, Integer rows, Integer ebookCategoryId) {
        //分页
        PageHelper.startPage(page,rows);
        //查询
        List<EbookEntry> ebookEntryList = ebookEntryMapper.selectByCategoryId(ebookCategoryId);
        //分装PageInfo
        PageInfo<EbookEntry> pageInfo = new PageInfo<>(ebookEntryList);
        return null;
    }
}
